﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vara_Data_Layer.Vara_Data_Make_Model
{
    public class Vara_Data_Model_Controller : Vara_Data_IModel_Actions
    {
        private static List<Vara_Data_IModel> _models;
        public List<Vara_Data_IModel> Models
        {
            get
            {
                if (_models == null)
                    Refresh_Model_List();

                return _models;
            }
        }

        public List<Vara_Data_IModel> ReturnModelsByMake(Vara_Data_IMake Make)
        {
            return Models.FindAll(delegate(Vara_Data_IModel Model) { return Model.Make_ID == Make.ID; });
        }
        public Vara_Data_IModel ReturnModel(int ID)
        {
            Vara_Data_IModel ModelToReturn;
            ModelToReturn = Models.Find(delegate(Vara_Data_IModel Model) { return Model.ID == ID; });
            return ModelToReturn;
        }

        public void Refresh_Model_List()
        {
            DataTable DataTableModels;
            using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
            {
                DataTableModels = NewSqlConnector.ExecuteProcedure("Return_Models");
            }
            List<Vara_Data_IModel> ListOfModels = new List<Vara_Data_IModel>();
            foreach (DataRow R in DataTableModels.Rows)
            {
                Vara_Data_IModel ModelToAppend = new Vara_Data_Model(int.Parse(R["ID"].ToString()));
                ModelToAppend.Name = R["Name"].ToString();
                ModelToAppend.Make_ID = int.Parse(R["Makes_Id"].ToString());
                ListOfModels.Add(ModelToAppend);
            }
            _models = ListOfModels;
        }
    }
}
